package com.gitee.huxing.course.controller;

import com.alibaba.fastjson.JSONObject;
import com.gitee.huxing.course.vo.MessageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.context.annotation.Import;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Description:
 * @ProjectName: gdca-platform
 * @Package: com.gdca.cert.consume
 * @ClassName: KafkaConsumer
 * @Author: huxing
 * @DateTime: 2021-08-23 下午2:11
 */
@Slf4j
@Component
public class KafkaConsumer {

    @KafkaListener(topics = "test")
    public void consumer(ConsumerRecord<?, ?> record){
        Optional<?> kafkaMessage = Optional.ofNullable(record);
        if(kafkaMessage.isPresent()){
            Object message = kafkaMessage.get();
            System.out.println("--------- record: " + message);
            System.out.println("--------- message: " + record.value());
        }
    }

    /**
     * @Description: 消费到es数据库中
     * @Author: huxing
     * @param record
     * @Date: 2021/8/23 下午3:44
     **/
    @KafkaListener(topics = "gdca")
    public void consumerForEs(ConsumerRecord<?, ?> record){
        Optional<?> kafkaMessage = Optional.ofNullable(record);
        if(kafkaMessage.isPresent()){
            Object message = kafkaMessage.get();
            System.out.println("--------- record: " + message);
            System.out.println("--------- message: " + record.value());
            log.info("接收消息成功:: {}", JSONObject.toJSONString(record.value()));
        }
    }
}
